package com.wjy.game.utils;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.Collections;
import java.util.Scanner;

/**
 * @author wjy
 * @date 2025/5/23 10:34
 * @description MyBatis Plus代码生成
 */
@Slf4j
public class MyBatisPlusGenerator {

    private final static String YES = "y";

    /**
     * <p>
     * 读取控制台内容
     * </p >
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        log.info("{}：", tip);
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {

        /*数据库配置*/
        //数据库改成自己的
        DataSourceConfig dataSourceConfig =
                new DataSourceConfig
                        .Builder("jdbc:mysql://localhost:3306/five?useUnicode=true&characterEncoding=utf-8", "root", "123456")
                        .dbQuery(new MySqlQuery())
                        .schema("five")
                        .build();

        /*创建一共代码生成器对象*/
        AutoGenerator generator = new AutoGenerator(dataSourceConfig);


        String property = System.getProperty("user.dir");
        property = property.concat("/five-server");
        /*全局配置*/
        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .enableSwagger()
                .outputDir(property.concat("/src/main/java"))
                .author("wjy") //将作者改成自己
                .disableOpenDir()
                .build();

        generator.global(globalConfig);


        /*包配置(PackageConfig)*/
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent("com.wjy.game") //这里改成自己对应的包名
                .entity("entites")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("mapper.automatic")
// .controller("controller")
                .pathInfo(Collections.singletonMap(OutputFile.xml, property.concat("/src/main/resources/mapper/automatic")))
                .build();

        generator.packageInfo(packageConfig);

        String[] tables = scanner("请输入表名，多个英文逗号分割").split(",");
        if (tables.length == 0) {
            log.warn("未输入有效表名");
            return;
        }
        boolean isXml = YES.equalsIgnoreCase(scanner("是否生成Xml(y/n)："));
        boolean isService = YES.equalsIgnoreCase(scanner("是否生成Service(y/n)："));
        boolean isController = YES.equalsIgnoreCase(scanner("是否生成Controller(y/n)："));

        TemplateConfig templateConfig = new TemplateConfig.Builder()
                .xml(isXml ? "/templates/mapper.xml" : null)
                .service(isService ? "/templates/service.java" : null)
                .serviceImpl(isService ? "/templates/serviceImpl.java" : null)
                .controller(isController ? "/templates/controller.java" : null)
                .build();

        generator.template(templateConfig);

        /*策略配置*/
        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                .enableCapitalMode()
                .enableSkipView()
                .disableSqlFilter()
                .addInclude(tables)
                //.addTablePrefix("t_") //去掉表中的前缀
                .build();

        if (isController) {
            strategyConfig.controllerBuilder()
                    .enableRestStyle()
                    .formatFileName("%sController")
                    .build();
        }


        strategyConfig.entityBuilder()
                .enableChainModel()
                .enableLombok()
                .enableRemoveIsPrefix()
                .enableTableFieldAnnotation()
                .naming(NamingStrategy.underline_to_camel) //开启驼峰命名映射表中字段
                .columnNaming(NamingStrategy.underline_to_camel)
                .formatFileName("%s")
                .fileOverride()
                .build();

        if (isService) {
            strategyConfig.serviceBuilder()
                    .formatServiceFileName("I%sService")
                    .formatServiceImplFileName("%sServiceImpl")
                    .build();
        }

        strategyConfig
                .mapperBuilder()
                .superClass(BaseMapper.class)
                .enableBaseResultMap()
                .enableBaseColumnList()
                .formatMapperFileName("%sMapper")
                .formatXmlFileName("%sMapper")
                .build();


        generator.strategy(strategyConfig);

        /*执行代码生成器*/
        generator.execute();
    }
}
